<template>
    <div class="FromGood">
        <el-form :model="travel" ref="goodFrom" label-width="120px" class="demo-ruleForm">
            <el-form-item label="游记时间" prop="travelTime">
                <el-date-picker v-model="travel.travelTime" type="date" placeholder="选择日期">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="游记标题" prop="travelTitle">
                <el-input v-model="travel.travelTitle"></el-input>
            </el-form-item>
            <el-form-item label="游记地点" prop="travelAddress">
                <el-input v-model="travel.travelAddress"></el-input>
            </el-form-item>
            <el-form-item label="游记图片" prop="region">
                <el-upload action="#" ref="upload" :on-preview="handlePreview" :on-remove="handleRemove"
                    list-type="picture-card" :file-list="fileList" :http-request="handleFileUpload">
                    <i class="el-icon-plus"></i>
                </el-upload>
                <el-dialog :visible.sync="dialogVisible">
                    <img width="100%" :src="dialogImageUrl" alt="">
                </el-dialog>
            </el-form-item>
            <el-form-item label="游记正文" prop="text">
                <wangEditorCom @catchData="catchData" ref="editorRef" v-if="sonContent" :content="sonContent">
                </wangEditorCom>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm(1)">草稿</el-button>
                <el-button type="primary" @click="submitForm(2)">保存</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import wangEditorCom from '../../components/upload/wangEditorCom.vue';
import { AlbumUpload } from '@/assets/api/fileUpload.js';
import { selectTravelDetail, updateTravelDetail } from '@/assets/api/travel.js'
export default {
    data() {
        return {
            sonContent: "",
            travelId: '',
            imageList: [],
            travel: {
                travelTitle: '',
                travelContent: '',
                travelImg: '',
                travelTime: '',
                travelAddress: '',
            },

            dialogImageUrl: '',
            dialogVisible: false,
            fileList:[]

        }
    },
    components: {
        wangEditorCom
    },
    mounted() {
        this.travelId = this.$route.params.travelId
        if (this.travelId != undefined && this.travelId != '') {
            this.showDetail()
        }
    },
    methods: {
        submitForm(type) {
            var imgStr = '';
            this.travel.status = type;
            if (this.imageList.length > 0) {
                imgStr = this.imageList.toString();
                this.travel.travelImg = imgStr;
            }
            updateTravelDetail(this.travel).then((resp) => {
                if (resp.code == 200) {
                    //关闭当前卡签页
                    this.$message.success("保存成功");
                    this.$store.dispatch("delVisitedViews", this.$route)
                    this.$router.push("/travel/travelList")
                }else{
                    this.$message.error(resp.message);
                }
            })
        },
        async showDetail() {
            selectTravelDetail(this.travelId).then((resp) => {
                if (resp.code == 200) {
                    this.travel = resp.data;
                    this.sonContent = resp.data.travelContent
                    if (this.travel.travelImg != '') {
                        this.imageList = this.travel.travelImg.split(",")
                    }
                    if (resp.data.travelImageList.length > 0) {
                        for (var i = 0; i < resp.data.travelImageList.length; i++) {
                            this.fileList.push({
                                "name": resp.data.travelImageList[i].pictureId
                                , "url": resp.data.travelImageList[i].pictureUrl
                            })
                        }
                    }
                }
            })
        },
        //获取富文本内容
        catchData(content, text) {
            this.travel.travelContent = content;
        },
        // 处理移除操作
        handleRemove(file, fileList) {
            var fileName = file.name;
            deleteFile(fileName).then((resp) => {
                if (resp.code == 200) {
                    this.imageList = this.imageList.filter(item => item != fileName)
                }
            })
        },
        // 处理预览操作
        handlePreview(file) {
            console.log(file);
        },
        // 处理文件上传操作
        handleFileUpload(file) {
            this.loading = true;
            var form = new FormData();
            // 添加键值对
            form.append("file", file.file);
            // 调用后端服务器的接口
            AlbumUpload(form).then((resp) => {
                this.dialogImageUrl = resp.data.pictureUrl;
                this.imageList.push(resp.data.pictureId)
                console.log(this.imageList)
            }).catch((e) => {
                this.$message.error(e.message);
                this.$refs.upload.clearFiles();
            })
        }
    }
}
</script>

<style scoped>
.FromGood {
    width: 100%;
    min-height: 100%;
    padding: 40px;
    box-sizing: border-box;
}
</style>